#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_buffer_reference2 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require

layout(points) in;
layout(line_strip, max_vertices=96) out;
//layout(points, max_vertices=64) out;

#include "../../../../core/shader_color.h"
#include "../../../../core/shader_curve.h"
#include "../../../../core/shader_taster_curve.h"

//layout(std430, column_major, binding = DEF_BINDING_WIDGET_CurveIndex) readonly buffer U6_Matrix {
//	uint m_CurveIndex[];
//};



layout(location = 1) in VS_GS_VERTEX {
	uint vInstance;
}vertex_in[];


layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
	//float	gl_PointSize;
};



//vec4 v = vec4(0, 0, 0, 1);

void main(void){
	uint Instance = vertex_in[0].vInstance;
	


	S_CurvePoint s_curve, e_curve;
	vec4 Color[2];

	uint s_ID = Instance;
	uint e_ID = Instance + 1;

	s_curve = m_Curve[s_ID];
	e_curve = m_Curve[e_ID];

	Color[0] = f_glsl_buildColor(s_curve.ID_Color.y);

	int num = 16;
	if(s_curve.ID_Color.x != 0xffffffff){
		Color[1] = f_glsl_buildColor(e_curve.ID_Color.y);
		
		float mixColor = 1.0/num;
		for(uint i = 0; i <= num; ++i){
			vec4 vert = vec4(f_curve_cal_point(s_curve, e_curve, (i*mixColor)), 0, 1);
			vert.xy *= m_Scale;
			vert.xy += m_Loc;
			
			gl_Position = mat_ProjecView * vert;

			gColor = mix(Color[0], Color[1], mixColor * i);
			//gColor.rgb += vert.y / m_Scale.y;
			EmitVertex();
		}
	}
	/*else {
		vec4 vert = vec4(s_curve.L_point, 0, 1);
		vert.xy *= m_Scale;
		vert.xy += m_Loc;

		gl_Position = mat_ProjecView * vert;

		gColor = Color[0] + vec4(0, 0.3, 0, 0);
		gColor.rgb *= 0.01;
		EmitVertex();


		vert = vec4(s_curve.R_point, 0, 1);
		vert.xy *= m_Scale;
		vert.xy += m_Loc;
		gl_Position = mat_ProjecView * vert;

		gColor = Color[0] + vec4(0, 0.3, 0, 0);
		gColor.rgb *= 0.01;
		EmitVertex();
	}*/
	EndPrimitive();
	



	vec4 vert;
	if(bool(s_curve.Select_Type.x)){
		vert = vec4(s_curve.R_point, 0, 1);
		vert.xy *= m_Scale;
		vert.xy += m_Loc;
		gl_Position = mat_ProjecView * vert;
		gColor = vec4(0,0,0,1);
		EmitVertex();

		vert = vec4(s_curve.value, 0, 1);
		vert.xy *= m_Scale;
		vert.xy += m_Loc;
		gl_Position = mat_ProjecView * vert;
		gColor = vec4(0,0,0,1);
		EmitVertex();
		EndPrimitive();
	}
	

	if(bool(e_curve.Select_Type.x)){
		vert = vec4(e_curve.value, 0, 1);
		vert.xy *= m_Scale;
		vert.xy += m_Loc;
		gl_Position = mat_ProjecView * vert;
		gColor = vec4(0,0,0,1);
		EmitVertex();

		vert = vec4(e_curve.L_point, 0, 1);
		vert.xy *= m_Scale;
		vert.xy += m_Loc;
		gl_Position = mat_ProjecView * vert;
		gColor = vec4(0,0,0,1);
		EmitVertex();
		EndPrimitive();
	}

}



 